/*
 * SRMI (an easy-to-use zero-code remoting library for Java)
 * Copyright (C) 2007  Mohammad Nabil Hussein
 * mohammad (dot) nabil (dot) h (at) gmail (dot) com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package SRMI.RemotingServices.ClassGeneration;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;

/**
 * Generated the remote interface used by SRMI, and implemented
 * by the service wrapper and the stub adapter.
 * @author meemo
 */
public class RemoteInterfaceGenerator extends ClassGenerator
{  
    protected Class remoteExceptionClass;
    
    /**
     * @param interfacedClass The class to generate the remote interface for.
     */
    public RemoteInterfaceGenerator(Class interfacedClass)
    {
        this.setWrappedClass(interfacedClass);
        classKeyword = "interface";
        try
        {
            remoteExceptionClass = Class.forName("java.rmi.RemoteException");
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }
    }    

    protected void PrepareEmit()
    {
        PrepareDefaultWrapperEmit();
    }

    protected void EmitSupers()
    {
        file.print("extends java.rmi.Remote");
    }

    protected void EmitConstructor()
    {
        // none, we are an interface
    }

    protected void EmitFunction(Method m)
    {
        // declare every function except those from Object
        int mod = m.getModifiers();
        if(!Modifier.isPrivate(mod)
        && !Modifier.isStatic(mod) 
        && !Modifier.isFinal(mod)        
        )
        {            
            //EmitModifiers(mod);
            file.print("\t ");
            EmitType(m.getReturnType());
            file.print(" ");
            file.print(m.getName());
            file.print("(");
            EmitArgs(m, false);
            file.print(")");
            ArrayList<Class> exceps = new ArrayList<Class>();            
            for(Class c : m.getExceptionTypes())
                exceps.add(c);
            exceps.add(remoteExceptionClass);
            EmitExceptions(exceps.toArray(new Class[]{}));
            file.print(";\n");
        }
    }

    protected void EmitMembersVariables()
    {
        // none, we are an interface
    }

    protected String getGeneratedClassNameSuffix()
    {
        return "_generated_interface";
    }          
}
